/*
 * This file is Copyright 2002-2006 Loyola University Chicago,
 * Department of Computer Science and the Emerging Technologies Laboratory.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License via
 *    http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.citep.web.gwt.widgets;

import com.google.gwt.user.client.ui.*;
/*
 * A table which has cells that display text, but can also
 * display widget when clicked. This allows us to add editable
 * fields
 */
public class EditableTable extends DynaTable implements DataClickListener,EditableWidgetListener {

	private Widget [] columnWidgets;
	private boolean enabled;
	private int currColumn;
	private int currRow;
	private EditableTableListener listener;
	
	public void onEditComplete(String newText) {
		hideWidget(currRow, currColumn, newText);
		if(listener != null)
			listener.onCellChanged(currRow, currColumn, newText);
		
		currRow = currColumn = -1;
	}

	public void onEditCancelled(String oldText) {
		hideWidget(currRow, currColumn, oldText);
		
		currRow = currColumn = -1;
	}
	
	public void setEditableTableListener(EditableTableListener l) {
		listener = l;
	}
	
	protected void hideWidget(int row, int column, String text) {
		grid.clearCell(currRow, currColumn);
		grid.setText(currRow, currColumn, text);
		setEnabled(true);
		System.out.println("enabled: " + enabled);
	}
	
	protected void showWidget(int row, int column, String text) {
		//if we have a widget to show
		if(columnWidgets[column] != null) {
				
			grid.clearCell(row, column);
			grid.setText(row, column, "");
			
			grid.setWidget(row, column, columnWidgets[column]);
			((EditableWidget)columnWidgets[column]).initialize(text);
			//disable the grid while this widget is active
			setEnabled(false);
			
			currRow = row;
			currColumn = column;
		}
	}
	
	public void onDataElementClicked(int row, int column) {
		System.out.println("enabled: " + enabled);
		//this only works if the table is enabled
		if(enabled) {
			System.out.println("Data clicked:" + row + ":" + column);
			//we do row + 1 because the dataElementClicked event does not include the header
			String text = grid.getText(row+1, column);
			showWidget(row+1, column, text);
		}		
	}
	
	public EditableTable(DynaTableDataProvider provider, String[] columns, String gridStyleName, String headerRowStyle, String entryRowStyle, String[] columnStyles, int rowCount) {
		super(provider, columns, gridStyleName, headerRowStyle,entryRowStyle,columnStyles,rowCount);
		columnWidgets = new Widget[columns.length];
		
		//listen for clicks on each one of our cells
		setDataClickListener(this);
		setEnabled(true);
		
		currColumn = currRow = -1;
	}
	
	public void setColunmWidget(int idx, EditableWidget w) {
		columnWidgets[idx] = (Widget)w;
		w.setEditableWidgetListener(this);
	}
	
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	
	public boolean isEnabled() {
		return enabled;
	}
	
	public void setText(int row, int column, String text) {
		//we shouldn't change the text of a cell that is in "edit" mode
		if(enabled == true || row != currRow && column != currColumn)
			grid.setText(row, column, text);
	}
}
